import LogFactory from "log/LogFactory";
import {LoginSuccessData} from "type/LoginSuccessData";
import {getFromLocalStore, saveStringToLocalStore, saveToLocalStore} from "../LocalStorageUtill";
import IndexedDBStorage from "../IndexedDBStorage";
import DatabaseConnection from "../DatabaseConnection";
import AdminPageSetting from "type/AdminSetting";

const log = LogFactory.getLogger("UserStorage");
/** 用户本地存储类 */
export default class UserStorage{
    private static readonly USER_KEY: string = "login-user";
    private static readonly instance: UserStorage = new UserStorage();
    private readonly databaseConnection: DatabaseConnection = new DatabaseConnection();
    private readonly loginDB: IndexedDBStorage<LoginSuccessData> = new IndexedDBStorage<LoginSuccessData>(this.databaseConnection, "user-login-data");
    private readonly configDB: IndexedDBStorage<AdminPageSetting> = new IndexedDBStorage<AdminPageSetting>(this.databaseConnection, "user-config-data");

    private static config: AdminPageSetting | null = null;

    private static userConfig: UserPageSetting | null = null;

    /** 获取用户本地存储类实例 */
    public static getInstance(): UserStorage{
        return UserStorage.instance;
    }
    /**
     * 设置用户登录成功信息,到本地存储
     * @param user 用户登录成功信息
     */
    public async saveUser(user: LoginSuccessData): Promise<void>{
        saveToLocalStore(UserStorage.USER_KEY, user);
        saveStringToLocalStore("username", user.username);
        saveStringToLocalStore("Authorization", user.token);
        return this.loginDB.saveData(user);
    }
    /**
     * 获取用户登录成功信息,从本地存储
     */
    public getUser(): LoginSuccessData | null{
        return getFromLocalStore(UserStorage.USER_KEY);
    }
    /**
     * 从IndexedDB中获取用户登录成功信息
     */
    public async getUserFormIDB(): Promise<LoginSuccessData | null>{
        return await this.loginDB.getData();
    }
    /**
     * 获取后台管理页面设置数据
     */
    public async getAdminPageConfig(): Promise<AdminPageSetting>{
        return await this.configDB.getData() ?? this.getAdminPageConfigFromLocalStore();
    }
    /**
     * 保存后台管理页面设置数据到IndexedDB
     * @param data 需要保存的数据
     */
    public async saveAdminPageConfig(data: AdminPageSetting): Promise<void>{
        UserStorage.config = data;
        saveToLocalStore("admin-page-config", data);
        return this.configDB.saveData(data);
    }

    /**
     * 获取用户页面设置数据
     */
    public getUserPageConfigFromLocalStore(): UserPageSetting{
        // log.debug("获取用户页面配置", UserStorage.userConfig);
        if (UserStorage.userConfig){
            // log.debug("使用缓存的用户配置信息");
            return UserStorage.userConfig;
        }
        let config = getFromLocalStore<UserPageSetting>("user-page-config");
        if(!config){
            config = {
                username: UserStorage.instance.getUsername(),
                reimbursePageSize: 10,
                reimburseDetailsPageSize: 10
            };
        }
        UserStorage.userConfig = config;
        return config;
    }

    /**
     * 从本地存储中获取后台管理页面设置数据,如果没有则返回默认值
     */
    public getAdminPageConfigFromLocalStore(): AdminPageSetting{
       // log.debug("getAdminPageConfigFromLocalStore", UserStorage.config);
        if (UserStorage.config){
            // log.debug("使用缓存的系统配置信息");
            return UserStorage.config;
        }
        let config = getFromLocalStore<AdminPageSetting>("admin-page-config");
        if (!config){
            config = {
                username: UserStorage.instance.getUsername(),
                userPageSize: 10,
                rolePageSize: 10,
                menuPageSize: 10,
                pathPageSize: 10,
                defaultExpand: []
            };
        }
        UserStorage.config = config;
        return config;
    }

    /** 获取当前登录的用户名 */
    public getUsername(): string{
        return this.getUser()?.username || "";
    }

}